package service;


import java.io.IOException;
import java.util.Vector;

import model.Animal;
import network.Cmd_Message;
import network.ISession;
import network.Message;
import network.Session;

public class GlobalService {

    protected static GlobalService m_instance = null;
    ISession session = Session.gI();
    Message m;

    public void send() {
        session.sendMessage(m);
        m.cleanup();
    }

    public static GlobalService gI() {
        // TODO Auto-generated method stub
        if (m_instance == null) {
            m_instance = new GlobalService();
        }
        return m_instance;
    }
    
    //////////////GLOBAL SERVICE FUNCTIONS////////////////
    public void login(String username, String pass, String version) {
        // TODO Auto-generated method stub
        m = new Message(Cmd_Message.LOGIN);
        try {
            m.writer().writeUTF(username);
            m.writer().writeUTF(pass);
            m.writer().writeUTF(version);
        } catch (IOException e) {
        }
        send();
    }

    public void doRegister(byte providerId, String username, String pass) {
        m = new Message(Cmd_Message.REGISTER);
        try {
        	m.writer().writeByte(providerId);
            m.writer().writeUTF(username);
            m.writer().writeUTF(pass);
        } catch (IOException e) {
        }
        send();
    }

    public void getListItem(byte itemType) {
        m = new Message(Cmd_Message.GET_LIST_ITEM);
        try {
            m.writer().writeByte(itemType);

        } catch (IOException e) {
        }
        send();
    }
	
	public void getGardenData(int userID) {
        m = new Message(Cmd_Message.GET_GARDEN_DATA);
        try {
            m.writer().writeInt(userID);
        } catch (IOException e) {
        }
        send();
    }

    public void getListItemType() {
        m = new Message(Cmd_Message.LIST_ITEM_TYPE);
        send();
    }

    public void doBuyItem(int itemType, int itemID, int numItem, int typeBuy) {
        m = new Message(Cmd_Message.BUY_ITEM);
        try {
            m.writer().writeByte(itemType);
            m.writer().writeShort(itemID);
            m.writer().writeShort(numItem);
            m.writer().writeByte(typeBuy);

        } catch (IOException e) {
        }
        send();
    }

	public void disconnect() {
		// TODO Auto-generated method stub
		session.close();
	}

	public void getFriendList() {
        m = new Message(Cmd_Message.GET_FRIENDLIST);
        send();
    }

    public void doSearchFriend(int bsearchType, String sName, int iLev, int Money, int gender) {
        m = new Message(Cmd_Message.SEARCH_PLAYER);
        try {
            m.writer().writeByte(bsearchType);
            if (bsearchType == 0) // theo name
            {
                m.writer().writeUTF(sName);
            } else if (bsearchType == 1) // theo lev
            {
                m.writer().writeInt(iLev);
            } else if (bsearchType == 2)// theo money
            {
                m.writer().writeInt(Money);
            } else if (bsearchType == 3)// Theo gioi tinh
            {
                m.writer().writeByte(gender);
            }
        } catch (IOException e) {
        }
        send();
    }

    public void doAddFriend(int friendID, boolean accept) {
        m = new Message(Cmd_Message.ADD_FRIEND);
        try {
            m.writer().writeInt(friendID);
            m.writer().writeBoolean(accept);
        } catch (IOException e) {
        }
        send();
    }

    public void doRequestAddFriend(int friendID) {
        m = new Message(Cmd_Message.REQUEST_ADD_FRIEND);
        try {
            m.writer().writeInt(friendID);
        } catch (IOException e) {
        }
        send();
    }

    public void doDelFriend(int friendID) {
        m = new Message(Cmd_Message.DELETE_FRIEND);
        try {
            m.writer().writeInt(friendID);
        } catch (IOException e) {
        }
        send();
    }

    public void doChatWidth(int friendID, String content) {
        m = new Message(Cmd_Message.CHAT_WITH);
        try {
            m.writer().writeInt(friendID);
            m.writer().writeUTF(content);
        } catch (IOException e) {
        }
        send();
    }
	
	public void takeABathPet(int gardenID, int animalType, int animalID){
		m = new Message(Cmd_Message.DO_WATER);
        try {
        	m.writer().writeInt(gardenID);
            m.writer().writeShort(animalType);
            m.writer().writeShort(animalID);
        } catch (IOException e) {
        }
        send();
	}
	
	//option = 0: request price
	//option = 1: sell
	public void sellPet(int option, int gardenID, int animalType, int animalID){
		m = new Message(Cmd_Message.SELL_PET);
        try {
        	m.writer().writeByte(option);
        	m.writer().writeInt(gardenID);
            m.writer().writeShort(animalType);
            m.writer().writeShort(animalID);
        } catch (IOException e) {
        }
        send();	
	}
	
	public void harvestMoney(int gardenID, int animalType, int animalID){
		m = new Message(Cmd_Message.HARVEST_MONEY);
        try {
        	m.writer().writeInt(gardenID);
            m.writer().writeShort(animalType);
            m.writer().writeShort(animalID);
        } catch (IOException e) {
        }
        send();	
	}
	
	public void feedAnimal(int gardenID, int animalType, int animalID, int foodID){
		m = new Message(Cmd_Message.FEED_ANIMAL);
        try {
        	m.writer().writeInt(gardenID);
            m.writer().writeShort(animalType);
            m.writer().writeShort(animalID);
            m.writer().writeShort(foodID);
        } catch (IOException e) {
        }
        send();	
	}
	
	public void goToDoctor(int gardenID, int animalType, int animalID, int drugID){
		m = new Message(Cmd_Message.GO_DOCTOR);
        try {
        	m.writer().writeInt(gardenID);
            m.writer().writeShort(animalType);
            m.writer().writeShort(animalID);
            m.writer().writeShort(drugID);
        } catch (IOException e) {
        }
        send();		
	}
	
	public void hydridsAnimal(int gardenID, Vector vt){
		m = new Message(Cmd_Message.HYDRIDS_ANIMAL);
        try {
        	m.writer().writeInt(gardenID);
        	
        	int i = 0;
        	while(i < vt.size()){
        		Animal ani = (Animal)vt.elementAt(i);
	            m.writer().writeShort(ani.animalInfo.m_iId);
	            m.writer().writeShort(ani.m_iId);
	            
	            System.out.println("gender " + ani.m_iGender);
	            
	            i++;
	        }
        } catch (IOException e) {
        }
        send();		
	}
	
	public void setHandler(int gameType) {		
		m = new Message(Cmd_Message.SET_HANDLER);
		try {
			m.writer().writeByte(gameType);
		} catch (Exception e) {
		}
		send();
	}
	
	public void requestRoomList() {
		m = new Message(Cmd_Message.ROOM_LIST);
		send();
	}	
	public void requestBoardList(byte id) {
		m = new Message(Cmd_Message.BOARD_LIST);
		try {
			m.writer().writeByte(id);
		} catch (IOException e) {}
		send();
	}
	
	public void joinBoard(int roomID, int boardID, String pass){
		m = new Message(Cmd_Message.JOIN_BOARD);
		try {
			m.writer().writeByte(roomID);
			m.writer().writeByte(boardID);
			m.writer().writeUTF(pass);
		} catch (IOException e) {}
		send();
	}
	
	public void ready(byte roomID, byte boardID, boolean isReady) {
		m = new Message(Cmd_Message.READY);
		try {
			m.writer().writeByte(roomID);
			m.writer().writeByte(boardID);
			m.writer().writeBoolean(isReady);
		} catch (IOException e) {
		}
		send();
	}

	public void setMoney(byte roomID, byte boardID, int money) {
		m = new Message(Cmd_Message.SET_MONEY);
		try {
			m.writer().writeByte(roomID);
			m.writer().writeByte(boardID);
			m.writer().writeInt(money);
		} catch (IOException e) {
		}
		send();
	}

	public void setPassword(byte roomID, byte boardID, String pass) {
		m = new Message(Cmd_Message.SET_PASSWORD);
		try {
			m.writer().writeByte(roomID);
			m.writer().writeByte(boardID);
			m.writer().writeUTF(pass);
		} catch (IOException e) {
		}
		send();
	}

	public void kick(byte roomID, byte boardID, int kickID) {
		m = new Message(Cmd_Message.KICK_PLAYER);
		try {
			m.writer().writeByte(roomID);
			m.writer().writeByte(boardID);
			m.writer().writeInt(kickID);
		} catch (IOException e) {
		}
		send();
	}

	public void startGame(byte roomID, byte boardID) {
		m = new Message(Cmd_Message.START);
		try {
			m.writer().writeByte(roomID);
			m.writer().writeByte(boardID);
		} catch (IOException e) {
		}
		send();
	}

	public void leaveBoard(byte mIRoomID, byte mIBoardID) {
		// TODO Auto-generated method stub
		m = new Message(Cmd_Message.LEAVE_BOARD);
		try {
			m.writer().writeByte(mIRoomID);
			m.writer().writeByte(mIBoardID);
			
		} catch (IOException e) {
		}
		send();
	}
	
	///////////////QUEST PET SERVICE//////////////////
	public void submitPetPos(byte roomID, byte boardID, byte[][] data, int size){
		m = new Message(Cmd_Message.SUBMIT_DATA);
		try {
			m.writer().writeByte(roomID);
			m.writer().writeByte(boardID);
			
			
			int i, j;
			for(i = 0; i < size; i++){
				for(j = 0; j < size; j++){
					if(data[i][j] == 1){
						m.writer().writeByte(i);
						m.writer().writeByte(j);		
					}
				}
			}
			
		} catch (IOException e) {
		}
		send();	
	}
	
	public void submitPetPos(byte roomID, byte boardID, int[] row, int[] col){
		m = new Message(Cmd_Message.SUBMIT_DATA);
		try {
			m.writer().writeByte(roomID);
			m.writer().writeByte(boardID);
			
			int i;
			for(i = 0; i < row.length; i++){
				m.writer().writeByte(row[i]);
				m.writer().writeByte(col[i]);		
			}
			
		} catch (IOException e) {
		}
		send();	
	}

	public void shoot(byte roomID, byte boardID, int selectedRow,
			int selectedCol) {
		// TODO Auto-generated method stub
		m = new Message(Cmd_Message.SHOOT);
		try {
			m.writer().writeByte(roomID);
			m.writer().writeByte(boardID);
			m.writer().writeByte(selectedRow);
			m.writer().writeByte(selectedCol);
			
		} catch (IOException e) {
		}
		send();	
	}

	public void chatToBoard(byte roomID, byte boardID, String text) {
		m = new Message(Cmd_Message.CHAT_BOARD);
		try {
			m.writer().writeByte(roomID);
			m.writer().writeByte(boardID);
			m.writer().writeUTF(text);
		} catch (IOException e) {
		}
		send();
	}

	public void getListImage() {
		// TODO Auto-generated method stub
		m = new Message(Cmd_Message.GET_LIST_IMAGE);
		send();
	}
	
	public void getImage(int imgId) {
		// TODO Auto-generated method stub
		m = new Message(Cmd_Message.GET_IMAGE);
		try {
			m.writer().writeShort(imgId);
		} catch (IOException e) {
		}
		send();
	}

	public void doChangeAvatar(int idAvatar) {
		// TODO Auto-generated method stub
        m = new Message(Cmd_Message.CHANGE_AVATAR);
        try {
            m.writer().writeShort(idAvatar);
        } catch (IOException e) {
            e.printStackTrace();
        }
        send();
	}

	public void doGetDiary() {
		// TODO Auto-generated method stub
		m = new Message(Cmd_Message.DIARY);
        send();
	}
	public void doChangePass(String username, String oldPass, String newPass) {
		// TODO Auto-generated method stub
		m = new Message(Cmd_Message.CHANGE_PASSW);
        try {
        	m.writer().writeUTF(username);
        	m.writer().writeUTF(oldPass);
        	m.writer().writeUTF(newPass);
        } catch (IOException e) {
        }
        send();
	}

    public void doGetListSMS() {
        m = new Message(Cmd_Message.SMS_NAPTIEN);
        send();
    }

    public void doUpdateProfile(String fullName, String status, String mobile, int gender) {
        m = new Message(Cmd_Message.UPDATE_PROFILE);
        try {
            m.writer().writeUTF(fullName);
            m.writer().writeUTF(status);
            m.writer().writeUTF(mobile);
            m.writer().writeByte(gender);
           
            
        } catch (IOException e) {
        }
        send();
    }

	public void doPutToy(int idFarmOwner, int toyInfoId, int toyId,
			short tileX, short tileY) {
		// TODO Auto-generated method stub
		m = new Message(Cmd_Message.SET_TOYS);
        try {
        	m.writer().writeInt(idFarmOwner);
        	m.writer().writeShort(toyInfoId);
        	m.writer().writeShort(toyId);
        	m.writer().writeShort(tileX);
        	m.writer().writeShort(tileY);
            
        } catch (IOException e) {
        }
        send();
	}
	
	public void doSetPetPlayGame(int animalType, int animalId, int ownerId, int gameId){
		m = new Message(Cmd_Message.SET_PET_PLAYGAME);
        try {
        	m.writer().writeShort(animalType);
        	m.writer().writeShort(animalId);
        	m.writer().writeInt(ownerId);
        	m.writer().writeShort(gameId);
        } catch (IOException e) {
        }
        send();
	}
	
	public void doRequestChargeMoneySimCard(String userName, int type, String cardCode, String series) {
		m = new Message(Cmd_Message.PAYCARD);
        try {
        	m.writer().writeUTF(userName);
        	m.writer().writeShort(type);
        	m.writer().writeUTF(cardCode);
        	m.writer().writeUTF(series);
        } catch (IOException e) {
        }
        send();
	}
	
	/////////////////////FIGHTING SERVICE//////////////////////////
	public void doThrowDice(byte roomId, byte boardId){
		m = new Message(Cmd_Message.SHOOT);
        try {
        	m.writer().writeByte(roomId);
        	m.writer().writeByte(boardId);
        } catch (IOException e) {
        }
        send();
	}
	
	////////////////////BAU CUA SERVICE/////////////////////////////
	public void doLacBauCua(byte roomId, byte boardId){
		m = new Message(Cmd_Message.BEGIN_QUEST);
        try {
        	m.writer().writeByte(roomId);
        	m.writer().writeByte(boardId);
        } catch (IOException e) {
        }
        send();
	}

	public void doMoToBauCua(byte roomId, byte boardId){
		m = new Message(Cmd_Message.REQUEST_RESULT);
        try {
        	m.writer().writeByte(roomId);
        	m.writer().writeByte(boardId);
        } catch (IOException e) {
        }
        send();
	}
	
	public void doSubmitData(byte roomId, byte boardId, byte[] arrData){
		m = new Message(Cmd_Message.SUBMIT_DATA);
        try {
        	m.writer().writeByte(roomId);
        	m.writer().writeByte(boardId);
        	int i;
        	for(i = 0; i < arrData.length; i++){
        		if(arrData[i] > 0){
        			m.writer().writeByte(i);
        			m.writer().writeByte(arrData[i]);	
        		}
        	}
        } catch (IOException e) {
        }
        send();
	}

	///////////////////////STUDY ENGLISH SERVICE//////////////////////
	public void chatToTeacher(byte roomId, byte boardId, String text) {
		// TODO Auto-generated method stub
		m = new Message(Cmd_Message.REQUEST_RESULT);
        try {
        	m.writer().writeByte(roomId);
        	m.writer().writeByte(boardId);
        	m.writer().writeUTF(text);
        } catch (IOException e) {
        }
        send();
	}
	
	////////////////////QUAY SO//////////////////////////////////////
	public void requestPriceQuaySo() {
		// TODO Auto-generated method stub
		m = new Message(Cmd_Message.REQUEST_PRICE_QUAYSO);
        send();
	}

	public void doQuaySo(int type) {
		// TODO Auto-generated method stub
		m = new Message(Cmd_Message.QUAYSO);
		try {
        	m.writer().writeByte(type);
        } catch (IOException e) {
        }
        send();
	}

	public void getListCurrentQuest() {
		// TODO Auto-generated method stub
		m = new Message(Cmd_Message.REQUEST_CURRENT_QUEST);
        send();
	}

	public void doRequestForgetPass(byte providerId, String userName) {
		// TODO Auto-generated method stub
		m = new Message(Cmd_Message.GET_PASSWORD);
		try {
        	m.writer().writeByte(providerId);
        	m.writer().writeUTF(userName);
        } catch (IOException e) {
        }
        send();
	}

	public void doRequestTopPlayer(int type) {
		// TODO Auto-generated method stub
		m = new Message(Cmd_Message.TOP_PLAYER);
		try {
        	m.writer().writeShort(type);
        } catch (IOException e) {
        }
        send();
	}

	public void doRequestForumLink() {
		// TODO Auto-generated method stub
		m = new Message(Cmd_Message.FORUM_LINK);
		send();
	}
}
